<?php 
session_start();
require_once ("class/m_order.php");

	$CreateUser=$_SESSION['username'] ;
	$m = new M_Order();

	if (isset($_POST['m'])  &&  $_POST['m']!=""){
		if ($_POST['m']== 'detail'){
			if(isset($_POST['id']) && $_POST['id']!=""){ 
				$id=$_POST['id'];
				//$id=1;
				$result_main =$m->ViewHeader($id);
				$result_detail=$m->getViewDataDetail($id);
				
				$rows=$result_detail->RecordCount();
				$h="";
				if ($rows>0){
					while($row = $result_detail->FetchRow()){
						$h.="<tr>".getProductTableDetail($row)."</tr>"; 
					}
				}
				
				$re=array('header'=>json_encode($result_main),
						'html'=>$h,
						'rows'=>$rows
						);
						
				echo json_encode($re);
			}else{
				echo "Errro";
			}
		}else if ($_POST['m']=='running'){
				$no=$m->getRunning();
				echo "OR_".$_SESSION['branchcode'].date( "ymd" ).str_repeat('0', 3-strlen($no) ).$no;
			
		}else if ($_POST['m']=='HoAmt'){
			$pid = $_POST['p'];
			$amt=$m->getStockHO($pid);
			echo $amt;
		//******************************
		//****   Edit/ Add  Product Detail
		//******************************			
		}else   if ($_POST['m']== 'p_detail'){
			if(isset($_POST['id']) && $_POST['id']!=""){ 
				$id=$_POST['id'];
				$amt=$_POST['amt'];
				//$id=1;
				$row=$m->getProductDetail($id,$amt);
				
				echo "<tr>".getProductTableDetail($row)."</tr>"; //json_encode($m->getProductDetail($id,$amt));
			}else{
				echo "Errro";
			}
			
		//******************************
		//****       Add
		//******************************					
		}else if ($_POST['m']== 'add'){
			$ar= $_POST['var'];
			$ar["CreatedBy"]=$CreateUser;
			$ar["CreatedDate"]=date( "Y/m/d H:i:s" );
			$ar["Branch"]=$_SESSION['bid'];
			$id=$m->InsertData($ar);
		 
			$list=$ar['Detail'];
			$sql="Insert Into tb_order_product_detail (OrderID, Product, Amount_s) values ";
			$sqlDetail = "";
			
			 foreach ($list as $key => $val) {

				$sqlDetail.=",(".$id.",".$key.",".$list[$key].") ";
			 }
			//remove first comma
			$sqlDetail = substr($sqlDetail,1);
			//Excute
			$m->Excute($sql.$sqlDetail);					 

			echo "<tr>".getTable($m->getDataByID($id),$_POST['rows']+1)."</tr>"; 


		//******************************
		//****       Edit
		//******************************					
		}else if ($_POST['m']=='edit'){
			if (isset($_POST['var'])){
				$ar=$_POST['var'];
				$flag=isset($ar);
				$id=$_POST['id'];
				
				$ar["UpdatedBy"]=$CreateUser;
				$ar["UpdatedDate"]=date( "Y/m/d H:i:s" );
				
				$list=$ar['Detail'];
				
				$sqlScript="select * from tb_order_product where ID='".$id."'";
			
				// Insert data
				$m->updateData($sqlScript, $ar);
				
				// clear data
				$m->delDataDetail( $id );
				
				$sql="Insert Into tb_order_product_detail (OrderID, Product, Amount_s) values ";
							
				foreach ($list as $key => $val) {
					//list($amt, $type) = split('/', $list[$key]);
					
					$sqlDetail.=",(".$id.",".$key.",".$list[$key]." ) ";
				}
				//remove first comma
				$sqlDetail = substr($sqlDetail,1);
				//Excute
				$m->Excute($sql.$sqlDetail);	
			
				echo  getTable($m->getDataByID($id),$_POST['rows']);
			}
		//******************************
		//****       Delete
		//******************************					
		}else if ($_POST['m']=='del'){
			if(isset($_POST['var'])){
				$o=$_POST['var'];
				$m->delData($o["id"] ,$CreateUser);
			}
 			
		//******************************
		//****       update after checking
		//******************************			
		}else if ($_POST['m']== 'add_rc'){
			$id=$_POST['id'];
			$list=$_POST['var'];
			$sql="";
			 foreach ($list as $key => $val) {
				$sql=" Update tb_order_product_detail Set ";
				$sql.=" Amount_rc =" .$list[$key];
				$sql.=" Where ID=".$key."; ";
			 	$m->Excute($sql);					 
			 }
			$sqlupdate="Update tb_order_product "
							." set Status=1 "
							.", ReceivedDate=Now() "
							.", ReceivedBy='".$CreateUser."'"
							." Where ID=".$id;					 
			
			 $m->Excute($sqlupdate);
			echo "completed";
		//******************************
		//****       Search
		//******************************
		}else if ($_POST['m']== 'search'){
			$code=$_POST['code'];
			
			if ($_SESSION['branchtype'] ==0)
				$branchid = $_SESSION['bid'] ;
			else 
				$branchid=$_POST['branchid'];
			
			$datefrom=$_POST['datefrom'];
			$dateto=$_POST['dateto'];
			$status=$_POST['status'];
			
			$page=$_POST['page'];
			
			$page=isset($page)?$page:1;
			
			$result=$m->getData($code,$branchid,$datefrom,$dateto,$status,$page,$_SESSION['branchtype'] );
			$rows=$m->getRowCount();
			$i=1 + (($page-1) * $m->getListPage());
			$html="";
			$style="";
			if($rows>0){
				while($row = $result->FetchRow()){
					if ($row['Flage'] != '0'){
						$style="style='color:red'";
					}else{
							$style="";
					}
					If($i%2 <> 0){
						
						$html .= "<tr class=row1 ".$style.">";
					} else{
						$html .= "<tr ".$style.">"; 
					}
					$html .= getTable($row,$i)."</tr>";
					$i++;
				}
			}else{
				$html="<tr>"
					."<td colspan='9' align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";
			}
			//Return result , rows , page
			$ar_result=array('data'=>$html,
							'rows'=>$rows,
							'totalpage'=>$m->getTotalPage());
			echo json_encode( $ar_result );
		}
		
	//******************************
	//****       Get combo data
	//******************************			
	}else{
		if (isset($_GET['c']) && $_GET['c'] !="" ){
			echo json_encode( $m->getDataCBO($_GET['c']));
		}		
	}
	
	function getProductTableDetail($arr){
			$html= "<td align='right'>".$arr["ProductID"]."</td>"
					."<td align='left' style='padding-left:3px'>".$arr["ProductName"] ."</td>"
					."<td align='center'>".$arr["Unit"]."</td>"
					."<td align='right'>".number_format($arr["Amount"],0,'.',',')."</td>"
					."<td align='center'><img src='images/remove16.png' style='cursor:pointer' id='btnRemove' border=0/></td>";
			return $html;
	}
 
	//for search
	function getTable($row,$i){
		global $orderScreen;
		$check="&nbsp;";
		$edit="&nbsp;";
		if ($_SESSION["permission"][$orderScreen]['Deleted']=='1') 	
			$delete="<img src='./images/remove.png' class='btnDel' style='cursor:pointer'  title='Delete' border='0'/>";
		else
			$delete="";
			
		if ($row["iStatus"]=="1"){
			$check="<img src='images/Send.png' class='btnSend' style='cursor:pointer'  title='Send Order' border='0'/>";
			$iStatus="<font style='font-weight:bold;color:green'>".$row["Status"]."</font>";
			$edit="<img src='images/edit.png' class='btnEdit' style='cursor:pointer'  title='Edit' border='0'/>";
			
		}else if ($row["iStatus"]=="2"){
			if ($_SESSION['branchtype']==0) {// Branch
				$check="<img src='images/view.png' class='btnView' style='cursor:pointer'  title='View' border='0'/>";
				$delete="&nbsp;";
			}
			else  //Ho
				$check="<img src='images/check_product.png' class='btnCheck' style='cursor:pointer'  title='Check order product' border='0'/>";
				
			$iStatus="<font style='font-weight:bold;color:blue'>".$row["Status"]."</font>";
		}else if ($row["iStatus"]=="3"){	
			if ($_SESSION['branchtype']==1) {// Ho
				$check="<img src='images/view.png' class='btnViewConfirm' style='cursor:pointer'  title='View confirm' border='0'/>";
				$delete="&nbsp;";
			}else {
				$check="<img src='images/confirm.gif' class='btnClose' style='cursor:pointer'  title='Confirm recieve product' border='0'/>";
				$delete="&nbsp;";
			}
			$iStatus="<font style='font-weight:bold;color:blue'>".$row["Status"]."</font>";	
			
		}else{
			$iStatus="<font style='font-weight:bold;color:#666'>".$row["Status"]."</font>";		
			$check="<img src='images/view.png' class='btnViewClose' style='cursor:pointer'  title='View confirm' border='0'/>";
		//	$delete="&nbsp;";
		}
	 
		$html .="<td align='right'>".$i."</td>";
		$html .="<td style='display:none'>".$row['ID']."</td>";
		$html .="<td align='left'>".$row['OrderNo']."</td>";
		
		if ($_SESSION['branchtype']==1)
			$html .="<td align='left'>".$row['BranchName']."</td>";
		
		$html .="<td align='center'>".$iStatus."</td>";		
		$html .="<td align='center'>".$row['ChangeDate']."  by  ".$row['ChangeBy']."</td>";
		$html .="<td align='right'>".number_format($row['Amount'],0)."</td>";
		// $html .="<td align='right'>".number_format($row['Total'],0,'.',',')."</td>";
		$html .="<td align='center'>".$check."</td>";
		
		if ($_SESSION['branchtype']==0){
			if ($_SESSION["permission"][$orderScreen]['Edit']=='1') 	
				$html .="<td align='center'>".$edit."</td>";
			else 
				$html .="<td align='center'> </td>";			
		}
		$html .="<td align='center'>".$delete."</td>";
		
		return $html;
	}

?>